﻿using MenuItem = OMC.DataManagement.MenuItem;
using Folder = OMC.DataManagement.Folder;
using System.Collections.Generic;

namespace OMC.Classification
{
    /// <summary>
    /// an object of this class could manage all about the
    /// Prediction of the Result
    /// </summary>
    /// <remarks>
    /// Copyright 2012 Stefan Lütolf
    /// </remarks>
    public class Prediction
    {
        private double[] TheResultsArray;
        private TextClassifier TheTextClassifier;
        private const int MaxNumberOfFolders = 6;

        /// <summary>
        /// constructor to create a Prediction object
        /// </summary>
        /// <param name="inTextClassifier">The TextClassifier</param>
        /// <param name="inResults">a double Array of Results</param>
        public Prediction(TextClassifier inTextClassifier, ref double[] inResults)
        {
            this.TheResultsArray = inResults;
            this.TheTextClassifier = inTextClassifier;
        }

        /// <summary>
        /// to make a prediction, which classes passes
        /// </summary>
        /// <param name="inButtonList">a referenced list, to fill in the predicted class</param>
        /// <param name="inFolderList">a referenced list of the folderlist</param>
        public void makePredictedButtonList(ref List<MenuItem> inButtonList, ref List<Folder> inFolderList)
        {
            for (int i = 0; i < TheResultsArray.Length; i++)
            {

                string TheCategoryClassIndex = TheTextClassifier.getCategory(i);
                string TheMenuItemName = null;
                string TheMenuItemExtraInfo = null;
                foreach (Folder TheTempFolder in inFolderList)
                {
                    if (TheTempFolder.TheFolderStringIndex == TheCategoryClassIndex)
                    {
                        TheMenuItemName = TheTempFolder.TheFolderName;
                        TheMenuItemExtraInfo = TheTempFolder.TheFolderExtraInfo;
                    }
                }
                inButtonList.Add(new MenuItem(ref TheMenuItemName, ref TheMenuItemExtraInfo, (TheResultsArray[i] * 100)));
            }

            inButtonList.Sort(delegate(MenuItem Item1, MenuItem Item2) { return Item1.probability.CompareTo(Item2.probability); });
            inButtonList.Reverse();
            inButtonList.TrimExcess();
            if (inButtonList.Capacity > MaxNumberOfFolders)
            {
                inButtonList.RemoveRange(MaxNumberOfFolders - 1, inButtonList.Capacity - MaxNumberOfFolders);
            }
        }
    }
}